import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

@SuppressWarnings("serial")
public class Elastne_Porge extends JPanel implements ActionListener {

	JButton alusta, tagasi, algusesse, test;
	JTextField kiirus1, kiirus2, mass1, mass2;
	String abi = "", abi_1 = "", abi_2 = "";
	double k11, k22, m11, m22, speed1, speed2;
	JLabel kysimus, kysimus2, kysimus3, kysimus4;
	int ese, ese1_alg, ese2_alg, w, h;
	JPanel ylemine, sisu, andmed;
	Timer timer;
	int kiirus = 100;
	boolean k2ib, kokkup6rge;

	public Elastne_Porge() {

		setLayout(new BorderLayout(3, 3));

		timer = new Timer(kiirus, new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (!kokkup6rge) {
					ese1_alg = ese1_alg + (int) ((double) w / (w / 2.) * k11);
					ese2_alg = ese2_alg - (int) ((double) w / (w / 2.) * k22);
					if (ese1_alg >= ese2_alg - w / 12) {
						kokkup6rge = true;
						speed1 = (k11 * (m11 - m22) + 2 * m22 * (-k22))
								/ (m11 + m22);
						speed2 = ((-k22) * (m22 - m11) + 2 * m11 * k11)
								/ (m11 + m22);
					}
				}
				if (kokkup6rge) {

					ese1_alg = ese1_alg
							+ (int) ((double) w / (w / 2.) * speed1);
					if ((int) ((double) w / (w / 2.) * speed1) < 1
							&& speed1 != 0 && speed1 > 0)
						ese1_alg = ese1_alg + 1;
					if ((int) ((double) w / (w / 2.) * speed1) > -1
							&& speed1 != 0 && speed1 < 0)
						ese1_alg = ese1_alg - 1;
					ese2_alg = ese2_alg
							+ (int) ((double) w / (w / 2.) * speed2);
					if ((int) ((double) w / (w / 2.) * speed2) < 1
							&& speed2 != 0 && speed2 > 0)
						ese2_alg = ese2_alg + 1;
					if ((int) ((double) w / (w / 2.) * speed2) > -1
							&& speed2 != 0 && speed2 < 0)
						ese2_alg = ese2_alg - 1;
				}
				repaint();

			}

		});
		ylemine = new JPanel();
		ylemine.setLayout(new FlowLayout(FlowLayout.LEFT));
		tagasi = new JButton("<< TAGASI");
		tagasi.addActionListener(this);
		ylemine.add(tagasi);
		ylemine.setBorder(BorderFactory.createEtchedBorder());
		add(ylemine, BorderLayout.NORTH);
		sisu = new JPanel() {

			public void paintComponent(Graphics g) {
				w = getWidth();
				h = getHeight();
				ese = h / 2 - h / 10;

				if (kokkup6rge
						&& !((Math
								.abs((double) ((int) (speed1 * 100.)) / 100.0) < 0.01 && speed1 != 0.00))) {
					abi_1 = "Esimese eseme kiirus peale põrget: "
							+ (String
									.valueOf(Math
											.abs((double) ((int) (speed1 * 100.)) / 100.0)))
							+ " m/s";
				} else if (kokkup6rge
						&& ((Math.abs((double) ((int) (speed1 * 100.)) / 100.0) < 0.01 && speed1 != 0.00))) {
					abi_1 = "Esimese eseme kiirus peale põrget: 0.01 m/s";
				}
				g.drawString(abi_1, w / 30, (h / 2 + h / 4));

				if (kokkup6rge
						&& !((Math
								.abs((double) ((int) (speed2 * 100.)) / 100.0) < 0.01 && speed2 != 0.00))) {
					abi_2 = "Teise eseme kiirus peale põrget: "
							+ (String
									.valueOf(Math
											.abs((double) ((int) (speed2 * 100.)) / 100.0)))
							+ " m/s";
				} else if (kokkup6rge
						&& ((Math.abs((double) ((int) (speed2 * 100.)) / 100.0) < 0.01 && speed2 != 0.00))) {
					abi_2 = "Teise eseme kiirus peale põrget: 0.01 m/s";
				}
				g.drawString(abi_2, w - w / 2, (h / 2 + h / 4));

				if ((ese1_alg > w + w / 5 || ese1_alg < 0 - w / 5)
						&& (ese2_alg > w + w / 5 || ese2_alg < 0 - w / 5)) {
					k2ib = false;
					kokkup6rge = false;
					alusta.setEnabled(true);
					timer.stop();
				}
				if (k2ib != true) {
					ese1_alg = w / 9;
					ese2_alg = w - w / 6;
				}

				g.setColor(Color.BLACK);
				g.fillRect(0, h / 2, w, h / 30);
				g.setColor(Color.GREEN);
				g.fillRect(ese1_alg, ese, w / 12, h / 10);
				g.setColor(Color.RED);
				g.fillRect(ese2_alg, ese, w / 12, h / 10);
				g.drawString(abi, w / 3, (h / 2 + h / 4));

				if (k2ib == true && !kokkup6rge) {
					g.setColor(Color.BLUE);
					g.drawString(kiirus1.getText().trim() + " m/s", (ese1_alg),
							(ese + h / 20));
					g.drawString(kiirus2.getText().trim() + " m/s", (ese2_alg),
							(ese + h / 20));
				}
				if (kokkup6rge) {
					g.setColor(Color.BLUE);
					if (Math.abs((double) ((int) (speed1 * 100.)) / 100.0) < 0.01) {
						g.drawString("0.01" + " m/s", ese1_alg, (ese + h / 20));
					} else {
						g.drawString(String.valueOf(Math
								.abs((double) ((int) (speed1 * 100.)) / 100.0))
								+ " m/s", ese1_alg, (ese + h / 20));
					}

					if (Math.abs((double) ((int) (speed2 * 100.)) / 100.0) < 0.01) {
						g.drawString("0.01" + " m/s", ese2_alg, (ese + h / 20));
					} else {
						g.drawString(String.valueOf(Math
								.abs((double) ((int) (speed2 * 100.)) / 100.0))
								+ " m/s", ese2_alg, (ese + h / 20));
					}
				}

			}
		};
		sisu.setBorder(BorderFactory.createEtchedBorder());
		add(sisu, BorderLayout.CENTER);

		andmed = new JPanel();
		andmed.setLayout(new GridLayout(15, 1, 1, 1));
		kiirus1 = new JTextField("5.0");
		kiirus2 = new JTextField("5.0");
		mass1 = new JTextField("2.5");
		mass2 = new JTextField("2.2");
		alusta = new JButton("Alusta");
		alusta.addActionListener(this);
		algusesse = new JButton("Algusesse");
		algusesse.addActionListener(this);
		kysimus = new JLabel("Eseme I kiirus(m/s):");
		kysimus2 = new JLabel("Eseme II kiirus(m/s):");
		kysimus3 = new JLabel("Eseme I mass(kg):");
		kysimus4 = new JLabel("Eseme II mass(kg):");
		andmed.add(kysimus);
		andmed.add(kiirus1);
		andmed.add(kysimus2);
		andmed.add(kiirus2);
		andmed.add(kysimus3);
		andmed.add(mass1);
		andmed.add(kysimus4);
		andmed.add(mass2);
		andmed.add(alusta);
		andmed.add(algusesse);
		andmed.setBorder(BorderFactory.createEtchedBorder());
		add(andmed, BorderLayout.EAST);

	}

	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand() == "<< TAGASI") {
			this.setVisible(false);
			nulli();
		}
		if (e.getActionCommand() == "Alusta") {
			try {
				abi = "";
				abi_1 = "";
				abi_2 = "";
				k11 = (double) ((int) (Double.parseDouble(kiirus1.getText()) * 100.)) / 100.;
				kiirus1.setText(String.valueOf(k11));
				k22 = (double) ((int) (Double.parseDouble(kiirus2.getText()) * 100.)) / 100.;
				kiirus2.setText(String.valueOf(k22));
				m11 = (double) ((int) (Double.parseDouble(mass1.getText()) * 100.)) / 100.;
				mass1.setText(String.valueOf(m11));
				m22 = (double) ((int) (Double.parseDouble(mass2.getText()) * 100.)) / 100.;
				mass2.setText(String.valueOf(m22));
				k2ib = true;
				alusta.setEnabled(false);
				timer.start();
			} catch (Exception evt) {
				abi = "Kontrolli oma sisestatud andmed üle, kuskil on viga !";
				repaint();
			}
		}
		if (e.getActionCommand() == "Algusesse") {
			nulli();
		}
	}
	public void nulli(){
		alusta.setEnabled(true);
		timer.stop();
		k2ib = false;
		kokkup6rge = false;
		abi_1 = "";
		abi_2 = "";
		repaint();
	}

}